package com.xucia.jsponic.datasource;

import java.security.acl.Acl;
import java.util.Iterator;

import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeObject;

import com.xucia.jsponic.data.ObjectId;
import com.xucia.jsponic.data.Persistable;
import com.xucia.jsponic.data.PersistableImpl;

/**
 * This provides an interface for a persisted object to be initialized from a data source 
 * @author Kris Zyp
 *
 */
public interface PersistableInitializer {
	/**
	 * If this is called, it must be called first. This only needs to be called if a custom class/object is needed
	 * @param object - the new object to be initialized
	 */
	void initializeObject(PersistableImpl object);
	/**
	 * This should be called to indicate that the new data object should be a list and provides an iterator to populate the list
	 * The iterator may not go through all the values in the list at the initial load, but may wait until they are needed (which may be much later)
	 * @param iterator
	 */
	void initializeList(Iterator iterator);
	/**
	 * This should be called to indicate that the new object should be function 
	 * @param source
	 * @param parentScope
	 */
	void initializeFunction(String source, boolean authorizedOnServer);
	/**
	 * This should be called to indicate that the new object should be created with give object id as the prototype 
	 * @param source
	 * @param parentScope
	 */
	void initializeByPrototype(ObjectId prototypeId);
	/**
	 * This should be called to initialize an object's acl. If this is not set, the object will default to readonly for all users
	 * @param source
	 * @param parentScope
	 */
	void setAcl(Acl acl);
	/**
	 * This should be called to initialize an object to inherit it's acl and schema from another object. 
	 * @param source
	 * @param parentScope
	 */
	void setParent(ObjectId objectToInheritFrom);
	/**
	 * This should be called to initialize an object's acl. If this is not set, the object will default to readonly for all users
	 * @param source
	 * @param parentScope
	 */
	void setPersistentAcl(ObjectId aclId);
	/**
	 * This should be called to initialize a property value
	 * @param source
	 * @param parentScope
	 */
	void setSchema(Persistable schema);
	/**
	 * This should be called to initialize a property value
	 * @param source
	 * @param parentScope
	 */
	void setProperty(String name, Object value);
	/**
	 * This should set approximately how long a browser can allow the content to be cached
	 * @param source
	 * @param parentScope
	 */
	void setCacheLevel(long time);
	/**
	 * This should be called to indicate that the initialization is completet
	 * @param source
	 * @param parentScope
	 */
	void finished();
	/**
	 * This can be called to return the newly initialized object
	 * @return the new data object
	 */
	Persistable getInitializingObject();
	public static final Class OBJECT_CLASS = NativeObject.class; 
	public static final Class LIST_CLASS = NativeArray.class; 
	public static final Class FUNCTION_CLASS = BaseFunction.class; 
}
